<?php
/**
* @file $Id: UserFields.php 161 2006-09-07 06:21:17Z doritojones $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/
?>

<script language=javascript>
function changeDataType(type)
{
	switch(type)
	{
		case 'radio':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<INPUT type=checkbox name=tables[new][DEFAULT_SELECTION] value=Y>';
		break;

		case 'text':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=10>';
		break;

		case 'select':
			document.getElementById('options_div').innerHTML = '<?php echo TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]','Pull-Down/Select Multiple Choices<BR>* one per line','rows=7 cols=40'); ?>';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=10>';
		break;

		case 'numeric':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=4>';
		break;

		case 'multiple':
			document.getElementById('options_div').innerHTML = '<?php echo TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]','Pull-Down/Select Multiple Choices<BR>* one per line','rows=7 cols=40'); ?>';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=10>';
		break;

		case 'date':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<INPUT type=text name=tables[new][DEFAULT_SELECTION] size=10><small>* YYYY-MM-DD</small>';
		break;

		case 'textarea':
			document.getElementById('options_div').innerHTML = '';
			document.getElementById('default_div').innerHTML = '<TEXTAREA name=tables[new][DEFAULT_SELECTION] cols=20 rows=4></TEXTAREA>';
		break;

		case 'log':
			document.getElementById('options_div').innerHTML = '<?php
			$div = false;
			$new = true;
			$header .= '<small>Setup the Logging Fields:</small><TABLE style="border-style:none none none none;" cellspacing=0 callpadding=0 border=1 width=100%><TR>';
			// COPY TO LOWER SECTION STARTING HERE 
			$header .= '<TD valign=top style="border-color:transparent;border-style:none solid solid none;" bgcolor=#333366><IMG SRC=assets/left_corner.gif><small><B><font color=#FFFFFF>'._('Use Field?').'</font></b></small>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Checkbox').'</font></small><BR>'.CheckBoxInput($RET['LOG_CHECKBOX'],'tables['.$_REQUEST['id'].'][LOG_CHECKBOX]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Pull-down').'</font></small><BR>'.CheckBoxInput($RET['LOG_SELECT'],'tables['.$_REQUEST['id'].'][LOG_SELECT]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Date').'</font></small><BR>'.CheckBoxInput($RET['LOG_DATE'],'tables['.$_REQUEST['id'].'][LOG_DATE]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Numeric').'</font></small><BR>'.CheckBoxInput($RET['LOG_NUMERIC'],'tables['.$_REQUEST['id'].'][LOG_NUMERIC]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 1').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT1'],'tables['.$_REQUEST['id'].'][LOG_TEXT1]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 2').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT2'],'tables['.$_REQUEST['id'].'][LOG_TEXT2]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 3').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT3'],'tables['.$_REQUEST['id'].'][LOG_TEXT3]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 4').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT4'],'tables['.$_REQUEST['id'].'][LOG_TEXT4]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Long Text').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXTAREA'],'tables['.$_REQUEST['id'].'][LOG_TEXTAREA]','','',$new).'</TD>';

			$header .= '</TR><TR>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;"><small><b>'._('Field Title').'</b></small></TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_CHECKBOX_TITLE'],'tables['.$_REQUEST['id'].'][LOG_CHECKBOX_TITLE]','','',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_SELECT_TITLE'],'tables['.$_REQUEST['id'].'][LOG_SELECT_TITLE]','','',$div).'<BR>';
			$header .= '<small><b>Pull-Down Choices:</b></small>'.TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]','* '._('one per line'),'cols=10 rows=3');
			$header .= '</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_DATE_TITLE'],'tables['.$_REQUEST['id'].'][LOG_DATE_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_NUMERIC_TITLE'],'tables['.$_REQUEST['id'].'][LOG_NUMERIC_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT1_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT1_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT2_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT2_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT3_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT3_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT4_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT4_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXTAREA_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXTAREA_TITLE]','','size=8',$div).'</TD>';
			// END COPY
			$header .= '</TR></TABLE>';
			echo $header;
			$header = '';
				 ?>';
				
			document.getElementById('default_div').innerHTML = 'N/A';
		break;
	}
}
</script>
<?php

DrawHeader(ProgramTitle());
$_FOCUS['allow_edit'] = true;

if($_REQUEST['tables'] && $_POST['tables'])
{
	$table = $_REQUEST['table'];
	foreach($_REQUEST['tables'] as $id=>$columns)
	{
		if($id!='new')
		{
			if($columns['CATEGORY_ID'] && $columns['CATEGORY_ID']!=$_REQUEST['category_id'])
				$_REQUEST['category_id'] = $columns['CATEGORY_ID'];

			$sql = "UPDATE $table SET ";

			foreach($columns as $column=>$value)
				$sql .= $column."='".str_replace("\'","''",$value)."',";
			$sql = substr($sql,0,-1) . " WHERE ID='$id'";
			$go = true;
		}
		else
		{
			$sql = "INSERT INTO $table ";
			
			if($table=='USER_FIELDS')
			{
				if($columns['CATEGORY_ID'])
				{
					$_REQUEST['category_id'] = $columns['CATEGORY_ID'];
					unset($columns['CATEGORY_ID']);
				}
				$id = DBGet(DBQuery("SELECT ".db_seq_nextval('USER_FIELDS_SEQ').' AS ID '.FROM_DUAL));
				$id = $id[1]['ID'];
				$fields = "ID,CATEGORY_ID,";
				$values = $id.",'".$_REQUEST['category_id']."',";
				$_REQUEST['id'] = $id;

				switch($columns['TYPE'])
				{
					case 'radio':
						DBQuery("ALTER TABLE USERS ADD CUSTOM_$id VARCHAR(1)");
					break;
					
					case 'text':
					case 'select':
						DBQuery("ALTER TABLE USERS ADD CUSTOM_$id VARCHAR(255)");
					break;
					
					case 'multiple':
						DBQuery("ALTER TABLE USERS ADD CUSTOM_$id VARCHAR(1000)");
					break;
					
					case 'numeric':
						DBQuery("ALTER TABLE USERS ADD CUSTOM_$id NUMERIC(10,2)");
					break;
					
					case 'date':
						DBQuery("ALTER TABLE USERS ADD CUSTOM_$id DATE");
					break;
					
					case 'textarea':
						DBQuery("ALTER TABLE USERS ADD CUSTOM_$id VARCHAR(5000)");
					break;
					
					case 'log':
					break;
				}
				if($columns['TYPE']!='log')
					DBQuery("CREATE INDEX CUSTOM_STAFF_IND$id ON USERS (CUSTOM_$id)");
			}
			elseif($table=='USER_FIELD_CATEGORIES')
			{
				$id = DBGet(DBQuery("SELECT ".db_seq_nextval('USER_FIELD_CATEGORIES_SEQ').' AS ID '.FROM_DUAL));
				$id = $id[1]['ID'];
				$fields = "ID,";
				$values = $id.",";
				$_REQUEST['category_id'] = $id;
			}
			
			$go = false;

			foreach($columns as $column=>$value)
			{
				if($value)
				{
					$fields .= $column.',';
					$values .= "'".str_replace("\'","''",$value)."',";
					$go = true;
				}
			}
			$sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';
		}

		if($go)
			DBQuery($sql);
	}
	unset($_REQUEST['tables']);
}

if($_REQUEST['modfunc']=='delete')
{
	if($_REQUEST['id'])
	{
		$field = DBGet(DBQuery("SELECT TYPE FROM USER_FIELDS WHERE ID='".$_REQUEST['id']."'"));
		if(DeletePrompt(_('user field')))
		{
			$id = $_REQUEST['id'];
			DBQuery("DELETE FROM USER_FIELDS WHERE ID='$id'");
			if($field[1]['TYPE']!='log')
				DBQuery("ALTER TABLE USERS DROP COLUMN CUSTOM_$id");
			else
				DBQuery("DELETE FROM USER_LOG_ENTRIES WHERE USER_FIELD_ID='".$id."'");
			$_REQUEST['modfunc'] = '';
			unset($_REQUEST['id']);
		}
	}
	elseif($_REQUEST['category_id'])
	{
		if(DeletePrompt(_('user field category and all fields in the category')))
		{
			$fields = DBGet(DBQuery("SELECT ID,TYPE FROM USER_FIELDS WHERE CATEGORY_ID='$_REQUEST[category_id]'"));
			foreach($fields as $field)
			{
				DBQuery("DELETE FROM USER_FIELDS WHERE ID='$field[ID]'");
				if($field['TYPE']!='log')
					DBQuery("ALTER TABLE USERS DROP COLUMN CUSTOM_$field[ID]");
				else
					DBQuery("DELETE FROM USER_LOG_ENTRIES WHERE USER_FIELD_ID='".$field['ID']."'");
			}
			DBQuery("DELETE FROM USER_FIELD_CATEGORIES WHERE ID='$_REQUEST[category_id]'");
			$_REQUEST['modfunc'] = '';
			unset($_REQUEST['category_id']);
		}
	}
}

if(!$_REQUEST['modfunc'])
{
	// CATEGORIES
	$sql = "SELECT ID,TITLE FROM USER_FIELD_CATEGORIES ORDER BY TITLE";
	$QI = DBQuery($sql);
	$categories_RET = DBGet($QI);

	if($_REQUEST['id']!='new' && $_REQUEST['category_id']!='new')
		$delete_button = "<INPUT type=button value="._('Delete')." onClick='javascript:window.location=\"Modules.php?modname=$_REQUEST[modname]&modfunc=delete&category_id=$_REQUEST[category_id]&id=$_REQUEST[id]\"'>";

	// ADDING & EDITING FORM
	if($_REQUEST['id'] && $_REQUEST['id']!='new')
	{
		$sql = "SELECT * FROM USER_FIELDS WHERE ID='$_REQUEST[id]'";
		$RET = DBGet(DBQuery($sql));
		$RET = $RET[1];
		$title = $RET['TITLE'];
	}
	elseif($_REQUEST['category_id'] && $_REQUEST['category_id']!='new' && $_REQUEST['id']!='new')
	{
		$sql = "SELECT TITLE
				FROM USER_FIELD_CATEGORIES
				WHERE ID='$_REQUEST[category_id]'";
		$RET = DBGet(DBQuery($sql));
		$RET = $RET[1];
		$title = $RET['TITLE'];
	}
	elseif($_REQUEST['id']=='new')
		$title = _('New User Field');
	elseif($_REQUEST['category_id']=='new')
		$title = _('New User Field Category');
	
	if($_REQUEST['id'])
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&category_id=$_REQUEST[category_id]";
		if($_REQUEST['id']!='new')
			echo "&id=$_REQUEST[id]";
		echo "&table=USER_FIELDS method=POST>";

		DrawHeader($title,$delete_button.'<INPUT type=submit value='._('Save').'>');
		$header .= '<TABLE cellpadding=3 width=100%>';
		$header .= '<TR>';

		$header .= '<TD>'.TextInput($RET['TITLE'],'tables['.$_REQUEST['id'].'][TITLE]',_('Field Name')) . '</TD>';

		// You can't change a user field type after it has been created
		if($_REQUEST['id']!='new')
		{
			$allow_edit = $_FOCUS['allow_edit'];
			$AllowEdit = $_FOCUS['AllowEdit'][$modname];
			$_FOCUS['allow_edit'] = false;
			$_FOCUS['AllowEdit'][$modname] = false;
		}
		$types = array('radio'=>_('Checkbox'),'text'=>_('Text'),'select'=>_('Pull-Down'),'numeric'=>_('Number'),'multiple'=>_('Select Multiple from Options'),'date'=>_('Date'),'textarea'=>_('Long Text'),'log'=>_('Logging Fields'));
		$header .= '<TD>' . SelectInput($RET['TYPE'],'tables['.$_REQUEST['id'].'][TYPE]',_('Data Type'),$types,false,'onchange="changeDataType(this.options[this.selectedIndex].value)"') . '</TD>';
		if($_REQUEST['id']!='new')
		{
			$_FOCUS['allow_edit'] = $allow_edit;
			$_FOCUS['AllowEdit'][$modname] = $AllowEdit;
		}
		foreach($categories_RET as $type)
			$categories_options[$type['ID']] = $type['TITLE'];
	
		if($_REQUEST['id']=='new')
			$header .= '<TD valign=bottom colspan=1><div id=default_div>'.CheckboxInput($RET['DEFAULT_SELECTION'],'tables['.$_REQUEST['id'].'][DEFAULT_SELECTION]','','',($_REQUEST['id']=='new'?true:false)).'</div><small>Default for new user</small></TD>';

		$header .= '<TD>' . SelectInput($RET['CATEGORY_ID']?$RET['CATEGORY_ID']:$_REQUEST['category_id'],'tables['.$_REQUEST['id'].'][CATEGORY_ID]',_('User Field Category'),$categories_options,false) . '</TD>';
		$header .= '</TR><TR id=second_row>';
		$colspan = 2;
		if($RET['TYPE']=='select' || $RET['TYPE']=='multiple')
			$header .= '<TD colspan=2><div id=options_div>'.TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]',_('Pull-Down/Select Multiple Choices').'<BR>* '._('one per line'),'rows=7 cols=40').'</div></TD>' . '<TD valign=bottom colspan=1><div id=default_div>'.CheckboxInput($RET['DEFAULT_SELECTION'],'tables['.$_REQUEST['id'].'][DEFAULT_SELECTION]','','',($_REQUEST['id']=='new'?true:false)).'</div><small>'._('Default for new user').'</small></TD>';
		elseif($RET['TYPE']=='log')
		{
			$new = true;
			$div = true;
			$header .= '<TD colspan=3><small>'._('Setup the Logging Fields').':</small><TABLE style="border-style:none none none none;" cellspacing=0 border=1 cellpadding=0 width=100%><TR>';
			// IF THE NEW FIELDS ARE MODIFIED IN THE JS SECTION AT TOP, PASTE IN CHANGES HERE
			$header .= '<TD valign=top style="border-color:transparent;border-style:none solid solid none;" bgcolor=#333366><IMG SRC=assets/left_corner.gif><small><B><font color=#FFFFFF>'._('Use Field?').'</font></b></small>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Checkbox').'</font></small><BR>'.CheckBoxInput($RET['LOG_CHECKBOX'],'tables['.$_REQUEST['id'].'][LOG_CHECKBOX]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Pull-down').'</font></small><BR>'.CheckBoxInput($RET['LOG_SELECT'],'tables['.$_REQUEST['id'].'][LOG_SELECT]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Date').'</font></small><BR>'.CheckBoxInput($RET['LOG_DATE'],'tables['.$_REQUEST['id'].'][LOG_DATE]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Numeric').'</font></small><BR>'.CheckBoxInput($RET['LOG_NUMERIC'],'tables['.$_REQUEST['id'].'][LOG_NUMERIC]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 1').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT1'],'tables['.$_REQUEST['id'].'][LOG_TEXT1]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 2').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT2'],'tables['.$_REQUEST['id'].'][LOG_TEXT2]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 3').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT3'],'tables['.$_REQUEST['id'].'][LOG_TEXT3]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Text 4').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXT4'],'tables['.$_REQUEST['id'].'][LOG_TEXT4]','','',$new).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;" bgcolor=#333366><small><font color=#FFFFFF>'._('Long Text').'</font></small><BR>'.CheckBoxInput($RET['LOG_TEXTAREA'],'tables['.$_REQUEST['id'].'][LOG_TEXTAREA]','','',$new).'</TD>';

			$header .= '</TR><TR>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;"><small><b>'._('Field Title').'</b></small></TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_CHECKBOX_TITLE'],'tables['.$_REQUEST['id'].'][LOG_CHECKBOX_TITLE]','','',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_SELECT_TITLE'],'tables['.$_REQUEST['id'].'][LOG_SELECT_TITLE]','','',$div).'<BR>';
			$header .= '<small><b>Pull-Down Choices:</b></small>'.TextAreaInput($RET['SELECT_OPTIONS'],'tables['.$_REQUEST['id'].'][SELECT_OPTIONS]','* '._('one per line'),'cols=10 rows=3');
			$header .= '</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_DATE_TITLE'],'tables['.$_REQUEST['id'].'][LOG_DATE_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_NUMERIC_TITLE'],'tables['.$_REQUEST['id'].'][LOG_NUMERIC_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT1_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT1_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT2_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT2_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT3_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT3_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXT4_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXT4_TITLE]','','size=8',$div).'</TD>';
			$header .= '<TD valign=top style="border-color:FFFFFF;border-style:none solid none solid;">'.TextInput($RET['LOG_TEXTAREA_TITLE'],'tables['.$_REQUEST['id'].'][LOG_TEXTAREA_TITLE]','','size=8',$div).'</TD>';
			// END PASTE
			$header .= '</TR></TABLE></TD>';
		}
		elseif($_REQUEST['id']=='new')
			$header .= '<TD colspan=4><div id=options_div></div></TD>';

		$header .= '</TR>';
		$header .= '</TABLE>';
	}
	elseif($_REQUEST['category_id'])
	{
		echo "<FORM action=Modules.php?modname=$_REQUEST[modname]&table=USER_FIELD_CATEGORIES";
		if($_REQUEST['category_id']!='new')
			echo "&category_id=$_REQUEST[category_id]";
		echo " method=POST>";
		DrawHeader($title,$delete_button.'<INPUT type=submit value='._('Save').'>');
		$header .= '<TABLE cellpadding=3 width=100%>';
		$header .= '<TR>';

		$header .= '<TD>' . TextInput($RET['TITLE'],'tables['.$_REQUEST['category_id'].'][TITLE]',_('Title')) . '</TD>';
		if($_REQUEST['category_id']=='new')
			$new = true;
	
		$header .= '</TR>';
		$header .= '</TABLE>';
	}
	else
		$header = false;

	if($header)
	{
		DrawHeader($header);
		echo '</FORM>';	
	}

	// DISPLAY THE MENU
	$LO_options = array('save'=>false,'search'=>false,'add'=>true);
	
	echo '<TABLE><TR>';
	
	if(count($categories_RET))
	{
		if($_REQUEST['category_id'])
		{
			foreach($categories_RET as $key=>$value)
			{
				if($value['ID']==$_REQUEST['category_id'])
					$categories_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
			}
		}
	}
		
	echo '<TD valign=top>';
	$columns = array('TITLE'=>_('Category'));
	$link = array();
	$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=$_REQUEST[modfunc]";
	$link['TITLE']['variables'] = array('category_id'=>'ID');
	$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&category_id=new";

	ListOutput($categories_RET,$columns,_('User Field Category'),_('User Field Categories'),$link,array(),$LO_options);
	echo '</TD>';
	
	// FIELDS
	if($_REQUEST['category_id'] && $_REQUEST['category_id']!='new' && count($categories_RET))
	{
		$sql = "SELECT ID,TITLE,TYPE FROM USER_FIELDS WHERE CATEGORY_ID='".$_REQUEST['category_id']."' ORDER BY TITLE";
		$fields_RET = DBGet(DBQuery($sql),array('TYPE'=>'_makeType'));
		
		if(count($fields_RET))
		{
			if($_REQUEST['id'] && $_REQUEST['id']!='new')
			{
				foreach($fields_RET as $key=>$value)
				{
					if($value['ID']==$_REQUEST['id'])
						$fields_RET[$key]['row_color'] = Preferences('HIGHLIGHT');
				}
			}
		}
			
		echo '<TD valign=top>';
		$columns = array('TITLE'=>_('User Field'),'TYPE'=>_('Data Type'));
		$link = array();
		$link['TITLE']['link'] = "Modules.php?modname=$_REQUEST[modname]&category_id=$_REQUEST[category_id]";
		$link['TITLE']['variables'] = array('id'=>'ID');
		$link['add']['link'] = "Modules.php?modname=$_REQUEST[modname]&category_id=$_REQUEST[category_id]&id=new";
	
		ListOutput($fields_RET,$columns,_('User Field'),_('User Fields'),$link,array(),$LO_options);

		echo '</TD>';
	}
	
	echo '</TR></TABLE>';
}

function _makeType($value,$name)
{
	$options = array('radio'=>_('Checkbox'),'text'=>_('Text'),'select'=>_('Pull-Down'),'date'=>_('Date'),'numeric'=>_('Number'),'textarea'=>_('Long Text'),'multiple'=>_('Select Multiple'),'log'=>_('Logging Fields'));
	return $options[$value];
}
?>